from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import random
from typing import List, Optional
from pydantic import BaseModel
import uvicorn

app = FastAPI()

# 允许跨域
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class NavLink(BaseModel):
    id: int
    title: str
    url: str
    icon: str
    description: str

class SubCategory(BaseModel):
    id: int
    title: str
    links: List[NavLink]

class Category(BaseModel):
    id: int
    title: str
    icon: str
    children: List[SubCategory]

# 模拟数据生成
def generate_mock_data():
    categories = [
        {
            'title': '影音娱乐', 
            'icon': 'fas fa-video',
            'subcategories': ['电影资源', '电视剧']
        },
        {
            'title': '游戏资源', 
            'icon': 'fas fa-gamepad',
            'subcategories': ['单机游戏', '网络游戏']
        },
        {
            'title': '编程开发', 
            'icon': 'fas fa-code',
            'subcategories': ['开发工具', '学习资源']
        },
        {
            'title': '音乐世界', 
            'icon': 'fas fa-music',
            'subcategories': ['音乐平台', '音乐下载']
        },
        {
            'title': '电子书籍', 
            'icon': 'fas fa-book',
            'subcategories': ['小说资源', '文档资料']
        }
    ]
    
    websites = [
        {'title': 'Netflix', 'url': 'https://netflix.com', 'icon': 'fas fa-play', 'description': '全球领先的流媒体平台'},
        {'title': 'YouTube', 'url': 'https://youtube.com', 'icon': 'fab fa-youtube', 'description': '最大的视频分享平台'},
        {'title': 'Bilibili', 'url': 'https://bilibili.com', 'icon': 'fas fa-video', 'description': '国内知名的视频弹幕网站'},
        {'title': 'Steam', 'url': 'https://store.steampowered.com', 'icon': 'fab fa-steam', 'description': '全球最大的游戏平台'},
        {'title': 'GitHub', 'url': 'https://github.com', 'icon': 'fab fa-github', 'description': '最大的代码托管平台'},
        {'title': 'Spotify', 'url': 'https://spotify.com', 'icon': 'fab fa-spotify', 'description': '全球最大的音乐流媒体平台'}
    ]
    
    result = []
    for i, cat in enumerate(categories):
        subcategories = []
        for j, subcat_name in enumerate(cat['subcategories']):
            websites_list = []
            for k in range(30):  # 每个子分类30个网站
                website = random.choice(websites).copy()
                website.update({
                    'id': f'{i+1}-{j+1}-{k+1}',
                    'description': f'{website["description"]} - {subcat_name} {k+1}'
                })
                websites_list.append(website)
            
            subcategories.append({
                'id': f'{i+1}-{j+1}',
                'title': subcat_name,
                'websites': websites_list
            })
        
        result.append({
            'id': i + 1,
            'title': cat['title'],
            'icon': cat['icon'],
            'children': subcategories
        })
    
    return result

@app.get("/api/navigation")
async def get_navigation():
    return {
        "code": 200,
        "message": "success",
        "data": generate_mock_data()
    }

if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) 